package com.telit.android.strategy.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.telit.android.constant.StrategyTypeConstants;
import com.telit.android.feign.UpmsFeignClient;
import com.telit.android.service.impl.UserInfoService;
import com.telit.android.strategy.SmsCodeStrategyInterface;
import com.telit.common.constant.RedisConstants;
import com.telit.common.constant.SmsTemplateConstants;
import com.telit.common.dto.SysShortUserDto;
import com.telit.common.enums.ResultCode;
import com.telit.common.exception.ForestBaseException;
import com.telit.common.util.AliSmsUtils;
import com.telit.common.util.RedisUtils;
import com.telit.common.vo.user.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * @Auther amy
 * @Describe 绑定手机号验证
 */
@Service(StrategyTypeConstants.STRATEGY_TYPE_SMS_CODE_CHANGE_PHONE_KEY)
public class ChangePhoneStorageStrategy implements SmsCodeStrategyInterface {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UpmsFeignClient upmsFeignClient;

    @Override
    public String sendSms(String phone, String code) throws ClientException {
        // 绑定手机号发送短信校验改手机号是否存在，判断是否已经绑定
        Result mobileResult = upmsFeignClient.queryUserByMobile(phone);
        if(mobileResult == null || mobileResult.getData() != null || !mobileResult.getCode().equals(ResultCode.SUCCESS.getCode())){
            throw new ForestBaseException("该手机号已绑定账号,请更换手机号绑定");
        }

        // todo 后期可能会换短信模板
        Map<String,String> map = new HashMap<>();
        map.put("code",code);
//        AliSmsUtils.sendSms(phone, JSON.toJSONString(map),SmsTemplateConstants.SMS_TEMPLATE_FORGET_PWD);
        AliSmsUtils.sendSms(phone, JSON.toJSONString(map),SmsTemplateConstants.SMS_TEMPLATE_CHANGE_PHONE);
        RedisUtils.set(RedisConstants.SMS_CODE_CHANGE_PHONE_REDIS_SUFFIX + phone,code,RedisConstants.SMS_CODE_VALIDATE_TIME);
        return null;
    }

    @Override
    public void checkCode(String phone, String code,Long userId) {
        if(userId == null || userId == 0L){
            throw new ForestBaseException("绑定手机的id不能为空");
        }

       // 校验手机号是否存在
        Result mobileResult = upmsFeignClient.queryUserByMobile(phone);
        if(mobileResult == null || mobileResult.getData() != null || !mobileResult.getCode().equals(ResultCode.SUCCESS.getCode())){
            throw new ForestBaseException("该手机号已绑定账号,请更换手机号绑定");
        }

        // 校验验证码
        String key = RedisConstants.SMS_CODE_CHANGE_PHONE_REDIS_SUFFIX + phone;
        userInfoService.checkSmsCode(key,code);

        // 修改手机号
        SysShortUserDto dto = new SysShortUserDto();
        dto.setId(userId);
        dto.setMobile(phone);
        Result result = upmsFeignClient.updateShortUser(dto);
        if(result == null || !result.getCode().equals(ResultCode.SUCCESS.getCode()) || result.getData().equals(false)){
            throw new ForestBaseException("手机号绑定失败");
        }
    }
}
